%GLOBAL CWD; %LET CWD = %SUBSTR(%SYSGET(SAS_EXECFILEPATH), 1, %EVAL(%LENGTH(%SYSGET(SAS_EXECFILEPATH)) - %LENGTH(%SYSGET(SAS_EXECFILENAME)) - 1));
%INCLUDE "&CWD\TOOLS.SAS";

/* SET ELI & CLUSTER */
%LET ELI = FJ011;
%LET CLUSTER = 01B;

/* IDENTIFYING HANDLE */
%LET COM = &ELI._&CLUSTER.;

/* ITEM-SPECIFIC RIGHT HAND SIDE VARIABLES */
%LET RHS = 
    B1 C1 C2 D1 D2 D3 D4 F1 G99SP H99SP I99SP
    BRAND_A BRAND_B BRAND_C BRAND_D BRAND_E BRAND_F BRAND_G BRAND_H BRAND_J 
    BRAND_K BRAND_M BRAND_N BRAND_R GRADE_A VITAMIN_D VITAMIN_A MNAMT MNAMTSQ 
    ;

%LET STYR = 0304;
%LET FIRSTPER = 20&STYR.;
%LET LASTPER = 200709;
%LET STDUM = D&STYR.;
%LET RHYRS = D0305 -- D0709;

%LET NSPER = %COUNTMONTHS(&FIRSTPER, &LASTPER);
%LET NVEC = %EVAL(&NSPER + %COUNTWORDS(&RHS));

/* DEFINE ITEM-SPECIFIC PRICE EDITS */
%MACRO EDITS;   
    IF .01>FULLSAMP_EFFPR_WOTX THEN DELETE; *OUTLIER REMOVAL;
%MEND;

/* DEFINE ITEM-SPECIFIC DUMMIES */
%MACRO VARS;    
    B1=(B="B1"); B2=(B="B2");
    ARRAY BARRAY (2) B1-B2;
    DO COUNT=1 TO 2;
        IF B="" THEN BARRAY(COUNT)=.;
    END;

    C1=(C="C1"); C2=(C="C2"); C3=(C="C3");
    C99SP=(C="C99");
    ARRAY CARRAY (4) C1-C3 C99SP;
    DO COUNT=1 TO 4;
        IF C="" THEN CARRAY(COUNT)=.;
    END;

    D1=(D="D1"); D2=(D="D2"); D3=(D="D3"); D4=(D="D4");
    D99SP=(D="D99");
    ARRAY DARRAY (5) D1-D4 D99SP;
    DO COUNT=1 TO 5;
        IF D="" THEN DARRAY(COUNT)=.;
    END;

    F1=(F="F1"); F2=(F="F2");
    ARRAY FARRAY (2) F1-F2;
    DO COUNT=1 TO 2;
        IF F="" THEN FARRAY(COUNT)=.;
    END;

    G99SP=(G="G99"); 
    H99SP=(H="H99");
    I99SP=(I="I99");

    IF FIND(E99,'**********','I') THEN BRAND_A=1;ELSE BRAND_A=0;
    IF FIND(E99,'**********','I') THEN BRAND_B=1;ELSE BRAND_B=0;
    IF FIND(E99,'**********','I') THEN BRAND_C=1;ELSE BRAND_C=0;
    IF FIND(E99,'**********','I') THEN BRAND_D=1;ELSE BRAND_D=0;
    IF FIND(E99,'**********','I') THEN BRAND_E=1;ELSE BRAND_E=0;
    IF FIND(E99,'**********','I') THEN BRAND_F=1;ELSE BRAND_F=0;
    IF FIND(E99,'**********','I') THEN BRAND_G=1;ELSE BRAND_G=0;
    IF FIND(E99,'**********','I') THEN BRAND_H=1;ELSE BRAND_H=0;
    IF FIND(E99,'**********','I') THEN BRAND_J=1;ELSE BRAND_J=0;
    IF FIND(E99,'**********','I') THEN BRAND_K=1;ELSE BRAND_K=0;
    IF FIND(E99,'**********','I') THEN BRAND_M=1;ELSE BRAND_M=0;
    IF FIND(E99,'**********','I') THEN BRAND_N=1;ELSE BRAND_N=0;
    IF FIND(E99,'**********','I') THEN BRAND_P=1;ELSE BRAND_P=0;
    IF FIND(E99,'**********','I') THEN BRAND_Q=1;ELSE BRAND_Q=0;
    IF FIND(E99,'**********','I') THEN BRAND_R=1;ELSE BRAND_R=0;

    IF FIND(G99,'GRADE A','I') THEN GRADE_A=1;ELSE GRADE_A=0;
    IF FIND(H99,'GRADE A','I') THEN GRADE_A=1;
    IF FIND(I99,'GRADE A','I') THEN GRADE_A=1;
    IF FIND(J99,'GRADE A','I') THEN GRADE_A=1;

    IF FIND(G99,'VITAMIN D','I') THEN VITAMIN_D=1;ELSE VITAMIN_D=0;
    IF FIND(H99,'VITAMIN D','I') THEN VITAMIN_D=1;
    IF FIND(I99,'VITAMIN D','I') THEN VITAMIN_D=1;
    IF FIND(J99,'VITAMIN D','I') THEN VITAMIN_D=1;

    IF FIND(G99,'VITAMIN A','I') THEN VITAMIN_A=1;ELSE VITAMIN_A=0;
    IF FIND(H99,'VITAMIN A','I') THEN VITAMIN_A=1;
    IF FIND(I99,'VITAMIN A','I') THEN VITAMIN_A=1;
    IF FIND(J99,'VITAMIN A','I') THEN VITAMIN_A=1;
    
    *ELIMINATE QUOTES WITH MISSING SPEC VARIABLES;
    *INCLUDE APPROPRIATE LIST OF SPEC VARIABLES HERE AND ELSEWHERE;
    *MODIFY SIZE OF SPECARRAY AS NEEDED TO NUMBER OF YOUR SPEC VARIABLES PLUS 5;
    *29+5=34;
    ARRAY SPECARRAY (*) B1 B2 C1 C2 C3 D1 D2 D3 D4 D99SP F1 F2 G99SP H99SP I99SP
        BRAND_A BRAND_B BRAND_C BRAND_D BRAND_E BRAND_F BRAND_G BRAND_H BRAND_J 
        BRAND_K BRAND_M BRAND_N BRAND_P BRAND_Q BRAND_R GRADE_A VITAMIN_D VITAMIN_A
        DISCDEP--OTHSTORE
        ;
    DO COUNT=1 TO DIM(SPECARRAY);
        IF SPECARRAY(COUNT)=. THEN DELETE;
    END;
%MEND;

LIBNAME IN "&CWD\DATA";
LIBNAME OUT "&CWD\DATA";

FILENAME OUTLOGS "&CWD\OUTPUT\LOGINDEXES.XLS";
FILENAME OUTCONTS "&CWD\OUTPUT\LOGCONTRIBS.XLS";
FILENAME OUTDISCS "&CWD\OUTPUT\DISCOUNTS.XLS";
FILENAME OUTPCTLS "&CWD\OUTPUT\PCTLS.XLS";

%INCLUDE "&CWD\TABLESTOBUILD.SAS";
